package com.fivenations.relax;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
	public static final String EXTRA_IDPI="EXTRA_IDPI";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);	
		
		
		TableLayout eventTable = (TableLayout)findViewById(R.id.event_table);
		eventTable.addView(ShowData());

		
		try{
		String destPath = "/data/data/" + getPackageName()+"/databases/relax";
		File f = new File(destPath);
		if(!f.exists()){
			CopyDB(getBaseContext().getAssets().open("mydb"),
					new FileOutputStream(destPath));
		}
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e){
			e.printStackTrace();
		}
		
		//ShowData(null);
		//DBAdapter db = new DBAdapter(this);
		
		/*
//============ add an event =============
		long id;
		db.open();
		//id = db.insertRecord("meeting", "26/05/2013", "11:43", "Library");
		//id = db.insertRecord("meeting", "26/05/2013", "12:43", "NB");
		db.close();
		
	
//=========== get all records ===========	
		
		db.open();
		Cursor c = db.getAllRecords();	
		if(c.moveToFirst()){
			do{
				DisplayRecord(c);
				}while(c.moveToNext());
		}
		db.close();
		*/
	}
	
	public void CopyDB(InputStream inputStream, OutputStream outputStream) throws IOException{
			// copy 1 Kilobyte at a time
			byte[] buffer = new byte[1024];
			int length;
			while((length = inputStream.read(buffer)) > 0){
				outputStream.write(buffer, 0, length);
			}
			inputStream.close();
			outputStream.close();
	}
		
	public void DisplayRecord(Cursor c){
		Toast.makeText(this,
				"id: " + c.getString(0) + "\n" +
				"title: "+ c.getString(1) + "\n" +
				"date: " + c.getString(2),
				Toast.LENGTH_LONG).show();
	}
	
	public void addEvent2(View View){
		
	}

	public void onGotoEventForm (View view){
		Intent i = new Intent(view.getContext(), EventFormActivity.class);
		this.startActivity(i);
	}

	
	private TableLayout ShowData(){
		
		DBAdapter db = new DBAdapter(this);
		
		TableLayout tableLayout = new TableLayout(getApplicationContext());
		tableLayout.setVerticalScrollBarEnabled(true);
		TableRow tableRow;
		TextView textView;
		
		db.open();
		Cursor c = db.getAllRecords();
		int count = c.getCount();
		if( count == 0){
			tableRow  = new TableRow(getApplicationContext());
			textView = new TextView(getApplicationContext());	
			textView.setText("No data");
			tableRow.addView(textView);
			tableLayout.addView(tableRow); 
		}
		else{
			c.moveToFirst();
			
			
			TextView textView2,textView3,textView4;
			
			for(int j = 0; j < count; j++){
				tableRow = new TableRow(getApplicationContext());
				tableRow.setBackgroundColor(Color.LTGRAY);
				textView2 = new TextView(getApplicationContext());
			
				
				textView2.setText(c.getString(c.getColumnIndex("title")));
				textView2.setTextColor(Color.BLACK);
				textView2.setTextSize((float)14);
				textView2.setPadding(20, 30, 20, 30);
			
				tableRow.addView(textView2);
				
				textView3 = new TextView(getApplicationContext());
				textView3.setText(c.getString(c.getColumnIndex("duedate")));
				textView3.setTextColor(Color.BLACK);
				textView3.setTextSize((float)14);
				textView3.setPadding(100, 30, 20, 30);
				tableRow.addView(textView3);
				
				textView4 = new TextView(getApplicationContext());
				textView4.setText(c.getString(c.getColumnIndex("time")));
				textView4.setTextColor(Color.BLACK);
				textView4.setTextSize((float)14);
				textView4.setPadding(100, 30, 20, 30);
				tableRow.addView(textView4);
				
				tableLayout.addView(tableRow);
				
				c.moveToNext();
			}
		}
		//setContentView(tableLayout);
		db.close();
		return tableLayout;
	}
}